package com.example.ticket.service.impl;

import com.example.ticket.entity.Ticket;
import com.example.ticket.mapper.TicketMapper;
import com.example.ticket.service.TicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class TicketServiceimpl implements TicketService {
    @Autowired
    TicketMapper ticketMapper;


    @Autowired
    RedisTemplate redisTemplate;


    @Override
    public Ticket insertTicket(Ticket ticket){
        int res=ticketMapper.insertTicket(ticket);
        if(res==1){
            System.out.println("票据添加成功");
            /*redisTemplate.opsForValue().set("ticket_"+ticket.getCompanyName(),ticket,20, TimeUnit.SECONDS);*/
        }
        return ticket;
    }

    @Override
    public Ticket updateTicketStatus(Ticket ticket){
        int res=ticketMapper.updateTicketStatus(ticket.getStatus(),ticket.getId());
        if(res==1){
            redisTemplate.opsForValue().set("ticket_"+ticket.getId(),ticket,20, TimeUnit.MINUTES);
            System.out.println("票据状态更新");
        }
        return ticket;
    }

    @Override
    public Ticket updateBack(Ticket ticket){
        int res=ticketMapper.updateBack(ticket);
        if(res==1){
            redisTemplate.opsForValue().set("ticket_"+ticket.getId(),ticket,20, TimeUnit.MINUTES);
            System.out.println("票据备注更新");
        }
        return ticket;
    }

    @Override
    public int deleteById(Integer id){
        int res=ticketMapper.deleteById(id);
        if(res==1){
            System.out.println("删除票据成功");
            redisTemplate.delete("ticket_"+id);
        }
        return res;
    }

    @Override
    public Ticket selectById(Integer id){
        Object object=redisTemplate.opsForValue().get("ticket_"+id);
        if(object!=null){
            return (Ticket) object;
        }else{
            Ticket ticket= ticketMapper.selectById(id);
            redisTemplate.opsForValue().set("ticket_"+id,ticket,20, TimeUnit.SECONDS);
            return ticket;
        }
    }

    @Override
    public List<Ticket> TicketList(){
        List<Ticket> list = ticketMapper.TicketList();
        /*        redisTemplate.opsForValue().set("user",list,1,TimeUnit.HOURS);*/
        return list;
    }

    @Override
    public Ticket updateTicketById(Ticket ticket){
        int res=ticketMapper.updateTicketById(ticket);
        if(res==1){
            redisTemplate.opsForValue().set("ticket_"+ticket.getId(),ticket,20, TimeUnit.MINUTES);
            System.out.println("编辑票据成功");
        }
        return ticket;
    }

    @Override
    public List<Ticket> selectMyTicket(Integer userId){
        List<Ticket> list = ticketMapper.selectMyTicket(userId);
        return list;
    }

    @Override
    public List<Ticket> selectMyTicketByStatus(Integer userId,Integer status){
        List<Ticket> list = ticketMapper.selectMyTicketByStatus(status,userId);
        return list;
    }

    @Override
    public List<Ticket> selectSTticket(Integer status){
        List<Ticket> list = ticketMapper.selectSTticket(status);
        return list;
    }

/*    @Override
    public List<Ticket> selectBycondition(String companyName,Integer id){
        List<Ticket> list=ticketMapper.selectBycondition(companyName,id);
        return list;
    }*/

    @Override
    public Ticket ChangeTicket(Ticket ticket){
        int res=ticketMapper.ChangeTicket(ticket);
        if(res==1) {
            redisTemplate.opsForValue().set("ticket_" + ticket.getId(), ticket,20, TimeUnit.MINUTES);
        }
        return ticket;
    }
}
